#include "Queue.h"
void QueueInit(Queue* pq)
{
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* pcur = pq->phead;
	while (pcur)
	{
		QueueNode* pnext = pcur->next;
		free(pcur);
		pcur = pnext;
	}
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

QueueNode* BuyNode(QueueDataType x)
{
	QueueNode* ret = (QueueNode*)malloc(sizeof(QueueNode));
	if (ret == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	ret->val = x;
	ret->next = NULL;
	return ret;
}
void QueuePush(Queue* pq, QueueDataType x)
{
	assert(pq);
	if (pq->phead == NULL)
	{
		QueueNode* newnode = BuyNode(x);
		pq->phead = pq->ptail = newnode;
		pq->size++;
	}
	else
	{
		QueueNode* newnode = BuyNode(x);
		pq->ptail->next = newnode;
		pq->ptail = newnode;
		pq->size++;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->size > 0);
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
		pq->size--;
		return;
	}
	else
	{
		QueueNode* del = pq->phead;
		pq->phead = pq->phead->next;
		free(del);
		pq->size--;
	}

}
QueueDataType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->phead);
	return pq->phead->val;
}
QueueDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->ptail);
	return pq->ptail->val;
}
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}
bool QueueEmpty(Queue* pq)
{
	return pq->size == 0;
}